using System;
using System.Configuration;
using Amazon.SimpleDB;
using Coditate.Common.Util;
using Coditate.TestSupport;
using NUnit.Framework;

namespace Coditate.Savant.System
{
    public class SystemTestUtils
    {
        public static SystemTestItem CreateItem()
        {
            var itemItem = new SystemTestItem
                {
                    DateValue = RandomDate(),
                    DoubleValue = RandomDouble(),
                    IntValue = RandomInt(),
                    StringValue = RandomData.AlphaNumericString(100, true),
                    LongStringValue = RandomData.AlphaNumericString(2000, true),
                    VersionValue = DateTime.Now
                };
            return itemItem;
        }

        public static double RandomDouble()
        {
            double d = TestData.RandomDouble();
            // constrain doubleValue to ensure it fits in default number of whole and decimal digits
            d = Math.Min(d, 9000000);
            d = Math.Round(d, 8);
            d *= TestData.RandomBool() ? -1 : 1;
            return d;
        }

        public static int RandomInt()
        {
            int i = TestData.RandomInt();
            i *= TestData.RandomBool() ? -1 : 1;
            return i;
        }

        public static DateTime RandomDate()
        {
            DateTime dt = TestData.RandomDateTime(DateTime.Now.AddYears(-100), DateTime.Now.AddYears(100));
            // constrain dateValue to supported precision
            dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute,
                              dt.Second, dt.Millisecond, DateTimeKind.Utc);
            return dt;
        }

        public static void AssertEqual(SystemTestItem item1, SystemTestItem item2)
        {
            var result = PropertyMatcher.AreEqual(item1, item2);
            Assert.IsTrue(result.Equal, result.Message);
        }

        public static SimpleSavant GetSavant()
        {
            string awsAccessKeyId = ConfigurationManager.AppSettings["AwsAccessKeyId"];
            string awsSecretAccessKey = ConfigurationManager.AppSettings["AwsSecretAccessKey"];

            var config = new SavantConfig
            {
                Cache = null
            };
            var aConfig = new AmazonSimpleDBConfig
            {
                ServiceURL = "http://sdb.amazonaws.com",
                //ServiceURL = "http://sdb.us-west-1.amazonaws.com"
            };
            var simpleDb = new AmazonSimpleDBClient(awsAccessKeyId, awsSecretAccessKey, aConfig);
            SimpleSavant savant = new SimpleSavant(simpleDb, config);
            return savant;
        }

        public static void Log(string message, ref DateTime start)
        {
            DateTime end = DateTime.Now;

            Console.WriteLine(message);
            Console.WriteLine("\t" + (end - start).TotalSeconds + " sec");
            start = DateTime.Now;
        }
    }
}